home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / tar.gnu / sprite / RCS / list.c,v < prev    next >
Encoding:
Text File  |  1992-03-29  |  26.2 KB  |  1,246 lines

  1. head     1.4;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.4
  10. date     92.03.28.17.32.05;  author kupfer;  state Exp;
  11. branches ;
  12. next     1.3;
  13.  
  14. 1.3
  15. date     92.03.05.21.38.01;  author rab;  state Exp;
  16. branches ;
  17. next     1.2;
  18.  
  19. 1.2
  20. date     90.06.28.15.35.36;  author rab;  state Exp;
  21. branches ;
  22. next     1.1;
  23.  
  24. 1.1
  25. date     90.03.21.22.51.30;  author rab;  state Exp;
  26. branches ;
  27. next     ;
  28.  
  29.  
  30. desc
  31. @@
  32.  
  33.  
  34. 1.4
  35. log
  36. @Restructure to remove some of the ALLOW_LONG_NAMES ifdefs.  Fix to
  37. allow both a long file name and a long link name.  Check for Posix
  38. archives.
  39. @
  40. text
  41. @/* List a tar archive.
  42.    Copyright (C) 1988 Free Software Foundation
  43.  
  44. This file is part of GNU Tar.
  45.  
  46. GNU Tar is free software; you can redistribute it and/or modify
  47. it under the terms of the GNU General Public License as published by
  48. the Free Software Foundation; either version 1, or (at your option)
  49. any later version.
  50.  
  51. GNU Tar is distributed in the hope that it will be useful,
  52. but WITHOUT ANY WARRANTY; without even the implied warranty of
  53. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  54. GNU General Public License for more details.
  55.  
  56. You should have received a copy of the GNU General Public License
  57. along with GNU Tar; see the file COPYING.  If not, write to
  58. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  59.  
  60. /*
  61.  * List a tar archive.
  62.  *
  63.  * Also includes support routines for reading a tar archive.
  64.  *
  65.  * this version written 26 Aug 1985 by John Gilmore (ihnp4!hoptoad!gnu).
  66.  *
  67.  * @@(#)list.c 1.31 11/5/87 - gnu
  68.  */
  69. #include <stdio.h>
  70. #include <ctype.h>
  71. #include <string.h>
  72. #include <sys/types.h>
  73. #include <sys/stat.h>
  74. #ifndef    MSDOS
  75. #include <sys/file.h>
  76. #endif    /* MSDOS */
  77. #include <assert.h>
  78. #include <sys/param.h>
  79.  
  80. #ifdef USG
  81. #include <sys/sysmacros.h>    /* major() and minor() defined here */
  82. #endif
  83.  
  84. char *ctime();                /* From libc.a */
  85.  
  86. #define    isodigit(c)    ( ((c) >= '0') && ((c) <= '7') )
  87.  
  88. #include "tar.h"
  89. #include "port.h"
  90.  
  91. extern FILE *msg_file;
  92.  
  93. long from_oct();            /* Decode octal number */
  94. void demode();                /* Print file mode */
  95.  
  96. union record *head;            /* Points to current archive header */
  97. struct stat hstat;            /* Stat struct corresponding */
  98. int head_standard;            /* Tape header is in ANSI format */
  99.  
  100. void print_header();
  101. void skip_file();
  102. void skip_extended_headers();
  103.  
  104. void get_long_name();
  105. void get_short_name();
  106.  
  107. extern char *quote_copy_string();
  108.  
  109.  
  110. /*
  111.  * Main loop for reading an archive.
  112.  */
  113. void
  114. read_and(do_something)
  115.     void (*do_something)();
  116. {
  117.     int status = 3;            /* Initial status at start of archive */
  118.     int prev_status;
  119.     extern time_t new_time;
  120.  
  121.     name_gather();            /* Gather all the names */
  122.     open_archive(1);        /* Open for reading */
  123.  
  124.     for(;;) {
  125.         prev_status = status;
  126.         status = read_header();
  127.         switch (status) {
  128.  
  129.         case 1:            /* Valid header */
  130.             /* We should decode next field (mode) first... */
  131.             /* Ensure incoming names are null terminated. */
  132.             head->header.name[NAMSIZ-1] = '\0';
  133.             saverec(&head);    /* hold onto the header */
  134.             userec(head); /* and skip past it in the archive */
  135.             get_names(head);
  136.             if (   !name_match(current_filename)
  137.                 || (f_new_files && hstat.st_mtime<new_time)
  138.                 || (f_exclude && check_exclude(current_filename))) {
  139.                 /*
  140.                  * Skip past the header and file in the 
  141.                  * archive
  142.                  */
  143.                 if (gnu_extended_header(head)) {
  144.                     skip_extended_headers();
  145.                 }
  146.                 if(head->header.linkflag != LF_DIR)
  147.                     skip_file((long)hstat.st_size);
  148.                 saverec((union record **)0);
  149.                 continue;
  150.  
  151.             }
  152.  
  153.             (*do_something)();
  154.             saverec((union record **)0);
  155.             continue;
  156.  
  157.             /*
  158.              * If the previous header was good, tell them
  159.              * that we are skipping bad ones.
  160.              */
  161.         case 0:            /* Invalid header */
  162.             userec(head);
  163.             switch (prev_status) {
  164.             case 3:        /* Error on first record */
  165.                 msg("Hmm, this doesn't look like a tar archive.");
  166.                 /* FALL THRU */
  167.             case 2:        /* Error after record of zeroes */
  168.             case 1:        /* Error after header rec */
  169.                 msg("Skipping to next file header...\n");
  170.             case 0:        /* Error after error */
  171.                     /* abort(); */
  172.                 break;
  173.             }
  174.             continue;
  175.  
  176.         case 2:            /* Record of zeroes */
  177.             userec(head);
  178.             status = prev_status;    /* If error after 0's */
  179.             if (f_ignorez)    
  180.                 continue;
  181.             /* FALL THRU */
  182.         case EOF:        /* End of archive */
  183.             break;
  184.         }
  185.         break;
  186.     };
  187.  
  188.     close_archive();
  189.     names_notfound();        /* Print names not found */
  190. }        
  191.  
  192.  
  193. /*
  194.  * Print a header record, based on tar options.
  195.  */
  196. void
  197. list_archive()
  198. {
  199.     extern char *save_name;
  200.  
  201.     /* Print the header record */
  202.     if (f_verbose) {
  203.         if (f_verbose > 1)
  204.             decode_header(head, &hstat, &head_standard, 0);
  205.         print_header();
  206.     }
  207.  
  208.     if(f_gnudump && head->header.linkflag==LF_DUMPDIR) {
  209.         size_t    size, written, check;
  210.         char    *data;
  211.         extern int errno;
  212.         extern long save_totsize;
  213.         extern long save_sizeleft;
  214.  
  215.         if(f_multivol) {
  216.             save_name = current_filename;
  217.             save_totsize=hstat.st_size;
  218.         }
  219.         for(size = hstat.st_size;size>0;size-=written) {
  220.             if(f_multivol)
  221.                 save_sizeleft=size;
  222.             data = findrec()->charptr;
  223.             if(data==NULL) {
  224.                 msg("EOF in archive file?");
  225.                 break;
  226.             }
  227.             written = endofrecs()->charptr - data;
  228.             if(written>size)
  229.                 written=size;
  230.             errno=0;
  231.             check=fwrite(data,sizeof(char), written, msg_file);
  232.             userec((union record *)(data+written - 1));
  233.             if(check!=written) {
  234.                 msg_perror("only wrote %ld of %ld bytes to file %s",
  235.                        check, written,current_filename);
  236.                 skip_file((long)(size)-written);
  237.                 break;
  238.             }
  239.         }
  240.         if(f_multivol)
  241.             save_name = 0;
  242.         fputc('\n',msg_file);
  243.         fflush(msg_file);
  244.         return;
  245.  
  246.     }
  247.     /* Check to see if we have an extended header to skip over also */
  248.     if (gnu_extended_header(head))
  249.         skip_extended_headers();
  250.  
  251.     if(f_multivol) {
  252.         save_name = current_filename;
  253.     }
  254.  
  255.     /* Skip to the next header on the archive */
  256.  
  257.     skip_file((long) hstat.st_size);
  258.  
  259.     if(f_multivol)
  260.         save_name = 0;
  261. }
  262.  
  263.  
  264. /*
  265.  * Read a record that's supposed to be a header record.
  266.  * Return its address in "head", and if it is good, the file's
  267.  * size in hstat.st_size.
  268.  *
  269.  * Return 1 for success, 0 if the checksum is bad, EOF on eof,
  270.  * 2 for a record full of zeros (EOF marker).
  271.  *
  272.  * You must always userec(head) to skip past the header which this
  273.  * routine reads.
  274.  */
  275. int
  276. read_header()
  277. {
  278.     register int    i;
  279.     register long    sum, recsum;
  280.     register char    *p;
  281.     register union record *header;
  282.     long    from_oct();
  283.  
  284.     header = findrec();
  285.     head = header;        /* This is our current header */
  286.     if (NULL == header) return EOF;
  287.     recsum = from_oct(8,  header->header.chksum);
  288.     sum = 0;
  289.     p = header->charptr;
  290.     for (i = sizeof(*header); --i >= 0;) {
  291.         /*
  292.          * We can't use unsigned char here because of old compilers,
  293.          * e.g. V7.
  294.          */
  295.         sum += 0xFF & *p++;
  296.     }
  297.  
  298.     /* Adjust checksum to count the "chksum" field as blanks. */
  299.     for (i = sizeof(header->header.chksum); --i >= 0;)
  300.         sum -= 0xFF & header->header.chksum[i];
  301.     sum += ' '* sizeof header->header.chksum;    
  302.  
  303.     if (sum == recsum) {
  304.         /*
  305.          * Good record.  Decode file size and return.
  306.          */
  307.         if (header->header.linkflag == LF_LINK)
  308.             hstat.st_size = 0;    /* Links 0 size on tape */
  309.         else
  310.             hstat.st_size = from_oct(1+12, header->header.size);
  311.         return 1;
  312.     }
  313.  
  314.     if (sum == 8*' ') {
  315.         /*
  316.          * This is a zeroed record...whole record is 0's except
  317.          * for the 8 blanks we faked for the checksum field.
  318.          */
  319.         return 2;
  320.     }
  321.  
  322.     return 0;
  323. }
  324.  
  325.  
  326. /* 
  327.  * Decode things from a file header record into a "struct stat".
  328.  * Also set "*stdp" to !=0 or ==0 depending whether header record is "Unix
  329.  * Standard" tar format or regular old tar format.
  330.  *
  331.  * read_header() has already decoded the checksum and length, so we don't.
  332.  *
  333.  * If wantug != 0, we want the uid/group info decoded from Unix Standard
  334.  * tapes (for extraction).  If == 0, we are just printing anyway, so save time.
  335.  *
  336.  * decode_header should NOT be called twice for the same record, since the
  337.  * two calls might use different "wantug" values and thus might end up with
  338.  * different uid/gid for the two calls.  If anybody wants the uid/gid they
  339.  * should decode it first, and other callers should decode it without uid/gid
  340.  * before calling a routine, e.g. print_header, that assumes decoded data.
  341.  */
  342. decode_header(header, st, stdp, wantug)
  343.     register union record    *header;
  344.     register struct stat    *st;
  345.     int    *stdp;
  346.     int    wantug;
  347. {
  348.  
  349.     long from_oct();
  350.  
  351.     st->st_mode = from_oct(8,  header->header.mode);
  352.     st->st_mtime = from_oct(1+12, header->header.mtime);
  353.     if(f_gnudump) {
  354.         st->st_atime = from_oct(1+12, header->header.atime);
  355.         st->st_ctime = from_oct(1+12, header->header.ctime);
  356.     }
  357.     
  358.     if (0==strcmp(header->header.magic, TMAGIC)) {
  359.         /* Unix Standard tar archive */
  360.         *stdp = 1;
  361.         if (wantug) {
  362. #ifdef NONAMES
  363.             st->st_uid = from_oct(8,  header->header.uid);
  364.             st->st_gid = from_oct(8,  header->header.gid);
  365. #else
  366.             st->st_uid = finduid(header->header.uname);
  367.             st->st_gid = findgid(header->header.gname);
  368. #endif
  369.         }
  370.         switch (header->header.linkflag) {
  371.         case LF_BLK: case LF_CHR:
  372.             st->st_rdev = makedev(from_oct(8, header->header.devmajor),
  373.                        from_oct(8, header->header.devminor));
  374.         }
  375.     } else {
  376.         /* Old fashioned tar archive */
  377.         *stdp = 0;
  378.         st->st_uid = from_oct(8,  header->header.uid);
  379.         st->st_gid = from_oct(8,  header->header.gid);
  380.         st->st_rdev = 0;
  381.     }
  382. }
  383.  
  384.  
  385. /*
  386.  * Quick and dirty octal conversion.
  387.  *
  388.  * Result is -1 if the field is invalid (all blank, or nonoctal).
  389.  */
  390. long
  391. from_oct(digs, where)
  392.     register int    digs;
  393.     register char    *where;
  394. {
  395.     register long    value;
  396.  
  397.     while (isspace(*where)) {        /* Skip spaces */
  398.         where++;
  399.         if (--digs <= 0)
  400.             return -1;        /* All blank field */
  401.     }
  402.     value = 0;
  403.     while (digs > 0 && isodigit(*where)) {    /* Scan til nonoctal */
  404.         value = (value << 3) | (*where++ - '0');
  405.         --digs;
  406.     }
  407.  
  408.     if (digs > 0 && *where && !isspace(*where))
  409.         return -1;            /* Ended on non-space/nul */
  410.  
  411.     return value;
  412. }
  413.  
  414.  
  415. /*
  416.  * Actually print it.
  417.  *
  418.  * Plain and fancy file header block logging.
  419.  * Non-verbose just prints the name, e.g. for "tar t" or "tar x".
  420.  * This should just contain file names, so it can be fed back into tar
  421.  * with xargs or the "-T" option.  The verbose option can give a bunch
  422.  * of info, one line per file.  I doubt anybody tries to parse its
  423.  * format, or if they do, they shouldn't.  Unix tar is pretty random here
  424.  * anyway.
  425.  *
  426.  * Note that print_header uses the globals <head>, <hstat>, and
  427.  * <head_standard>, which must be set up in advance.  This is not very clean
  428.  * and should be cleaned up.  FIXME.
  429.  */
  430. #define    UGSWIDTH    11        /* min width of User, group, size */
  431. #define    DATEWIDTH    19        /* Last mod date */
  432. static int    ugswidth = UGSWIDTH;    /* Max width encountered so far */
  433.  
  434. void
  435. print_header()
  436. {
  437.     char modes[11];
  438.     char *timestamp;
  439.     char uform[11], gform[11];    /* These hold formatted ints */
  440.     char *user, *group;
  441.     char size[24];        /* Holds a formatted long or maj, min */
  442.     long longie;        /* To make ctime() call portable */
  443.     int    pad;
  444.     char *name;
  445.     extern long baserec;
  446.  
  447.     if(f_sayblock)
  448.         fprintf(msg_file,"rec %10d: ",baserec + (ar_record - ar_block));
  449.     /* annofile(msg_file, (char *)NULL); */
  450.  
  451.     if (f_verbose <= 1) {
  452.         /* Just the fax, mam. */
  453.         char *name;
  454.         name=quote_copy_string(current_filename);
  455.         if(name==0)
  456.             name = current_filename;
  457.         fprintf(msg_file, "%s\n", name);
  458.         if(name!=current_filename)
  459.             free(name);
  460.     } else {
  461.         /* File type and modes */
  462.         modes[0] = '?';
  463.         switch (head->header.linkflag) {
  464.         case LF_VOLHDR:
  465.             modes[0]='V';
  466.             break;
  467.  
  468.         case LF_MULTIVOL:
  469.             modes[0]='M';
  470.             break;
  471.  
  472.         case LF_SPARSE:
  473.         case LF_NORMAL:
  474.         case LF_OLDNORMAL:
  475.         case LF_LINK:
  476.             modes[0] = '-';
  477.             if ('/' == current_filename[strlen(current_filename)-1])
  478.                 modes[0] = 'd';
  479.         break;
  480.  
  481.  
  482.         case LF_DUMPDIR:modes[0] = 'd'; break;
  483.         case LF_DIR:    modes[0] = 'd'; break;
  484.         case LF_SYMLINK:modes[0] = 'l'; break;
  485.         case LF_BLK:    modes[0] = 'b'; break;
  486.         case LF_CHR:    modes[0] = 'c'; break;
  487.         case LF_FIFO:    modes[0] = 'p'; break;    
  488.         case LF_CONTIG:    modes[0] = 'C'; break;
  489.         }
  490.  
  491.         demode((unsigned)hstat.st_mode, modes+1);
  492.  
  493.         /* Timestamp */
  494.         longie = hstat.st_mtime;
  495.         timestamp = ctime(&longie);
  496.         timestamp[16] = '\0';
  497.         timestamp[24] = '\0';
  498.  
  499.         /* User and group names */
  500.         if (*head->header.uname && head_standard) {
  501.             user  = head->header.uname;
  502.         } else {
  503.             user = uform;
  504.             (void)sprintf(uform, "%d", (int)hstat.st_uid);
  505.         }
  506.         if (*head->header.gname && head_standard) {
  507.             group = head->header.gname;
  508.         } else {
  509.             group = gform;
  510.             (void)sprintf(gform, "%d", (int)hstat.st_gid);
  511.         }
  512.  
  513.         /* Format the file size or major/minor device numbers */
  514.         switch (head->header.linkflag) {
  515.         case LF_CHR:
  516.         case LF_BLK:
  517.             (void)sprintf(size, "%d,%d",
  518.                     major(hstat.st_rdev),
  519.                     minor(hstat.st_rdev));
  520.             break;
  521.         case LF_SPARSE:
  522.             (void)sprintf(size, "%ld",
  523.                  from_oct(1+12, head->header.realsize));
  524.             break;
  525.         default:
  526.             (void)sprintf(size, "%ld", (long)hstat.st_size);
  527.         }
  528.  
  529.         /* Figure out padding and print the whole line. */
  530.         pad = strlen(user) + strlen(group) + strlen(size) + 1;
  531.         if (pad > ugswidth) ugswidth = pad;
  532.  
  533.         name = quote_copy_string(current_filename);
  534.         if(!name)
  535.             name=current_filename;
  536.         fprintf(msg_file, "%s %s/%s %*s%s %s %s %s",
  537.             modes,
  538.             user,
  539.             group,
  540.             ugswidth - pad,
  541.             "",
  542.             size,
  543.             timestamp+4, timestamp+20,
  544.             name);
  545.  
  546.         if(name!=current_filename)
  547.             free(name);
  548.         switch (head->header.linkflag) {
  549. #ifdef LF_RMTLINK
  550.         case LF_RMTLINK:
  551. #endif
  552.         case LF_SYMLINK:
  553.             name = quote_copy_string(current_linkname);
  554.             if(!name)
  555.                 name=current_linkname;
  556.             fprintf(msg_file, " -> %s\n", name);
  557.             if(name!=current_linkname)
  558.                 free(name);
  559.             break;
  560.  
  561.         case LF_LINK:
  562.             name = quote_copy_string(current_linkname);
  563.             if(!name)
  564.                 name=current_linkname;
  565.             fprintf(msg_file, " link to %s\n", name);
  566.             if(name!=current_linkname)
  567.                 free(name);
  568.             break;
  569.  
  570.         default:
  571.             fprintf(msg_file, " unknown file type '%c'\n",
  572.                 head->header.linkflag);
  573.             break;
  574.  
  575.         case LF_OLDNORMAL:
  576.         case LF_NORMAL:
  577.         case LF_SPARSE:
  578.         case LF_CHR:
  579.         case LF_BLK:
  580.         case LF_DIR:
  581.         case LF_FIFO:
  582.         case LF_CONTIG:
  583.             case LF_DUMPDIR:
  584. #ifdef LF_PSEUDODEV
  585.         case LF_PSEUDODEV:
  586. #endif
  587.             putc('\n', msg_file);
  588.             break;
  589.  
  590.         case LF_VOLHDR:
  591.             fprintf(msg_file, "--Volume Header--\n");
  592.             break;
  593.             
  594.         case LF_MULTIVOL:
  595.             fprintf(msg_file, "--Continued at byte %ld--\n",from_oct(1+12,head->header.offset));
  596.             break;
  597.         }
  598.     }
  599.     fflush(msg_file);
  600. }
  601.  
  602. /*
  603.  * Print a similar line when we make a directory automatically.
  604.  */
  605. void
  606. pr_mkdir(pathname, length, mode)
  607.     char *pathname;
  608.     int length;
  609.     int mode;
  610. {
  611.     char modes[11];
  612.     char *name;
  613.     extern long baserec;
  614.  
  615.     if (f_verbose > 1) {
  616.         /* File type and modes */
  617.         modes[0] = 'd';
  618.         demode((unsigned)mode, modes+1);
  619.  
  620.         if(f_sayblock)
  621.             fprintf(msg_file,"rec %10d: ",baserec + (ar_record - ar_block));
  622.         /* annofile(msg_file, (char *)NULL); */
  623.         name=quote_copy_string(pathname);
  624.         if(!name)
  625.             name=pathname;
  626.         fprintf(msg_file, "%s %*s %.*s\n",
  627.             modes,
  628.             ugswidth+DATEWIDTH,
  629.             "Creating directory:",
  630.             length,
  631.             pathname);
  632.         if(name!=pathname)
  633.             free(name);
  634.     }
  635. }
  636.  
  637.  
  638. /*
  639.  * Skip over <size> bytes of data in records in the archive.
  640.  */
  641. void
  642. skip_file(size)
  643.     register long size;
  644. {
  645.     union record *x;
  646.     extern long save_totsize;
  647.     extern long save_sizeleft;
  648.  
  649.     if(f_multivol) {
  650.         save_totsize=size;
  651.         save_sizeleft=size;
  652.     }
  653.  
  654.     while (size > 0) {
  655.         x = findrec();
  656.         if (x == NULL) {    /* Check it... */
  657.             msg("Unexpected EOF on archive file");
  658.             exit(EX_BADARCH);
  659.         }
  660.         userec(x);
  661.         size -= RECORDSIZE;
  662.         if(f_multivol)
  663.             save_sizeleft-=RECORDSIZE;
  664.     }
  665. }
  666.  
  667.  
  668. /*
  669.  *----------------------------------------------------------------------
  670.  *
  671.  * skip_extended_headers --
  672.  *
  673.  *    Skip over any extended header records in the archive.
  674.  *
  675.  * Results:
  676.  *    None.
  677.  *
  678.  * Side effects:
  679.  *    The archive is repositioned past any extended header records.
  680.  *
  681.  *----------------------------------------------------------------------
  682.  */
  683.  
  684. void
  685. skip_extended_headers()
  686. {
  687.     register union record *exhdr;
  688.  
  689.     assert(gnu_extended_header(head));
  690.     assert((head->header.isextended
  691.         & (XH_FILENAME|XH_LINKNAME|XH_SPARSE_FILE)) != 0);
  692.     assert((head->header.isextended
  693.         & ~(XH_FILENAME|XH_LINKNAME|XH_SPARSE_FILE)) == 0);
  694.  
  695.     /* 
  696.      * get_names should have already skipped over extended name 
  697.      * records, so the only thing to skip over would be sparse file 
  698.      * records.  (XXX This is a hack.)
  699.      */
  700.      if (head->header.isextended & XH_SPARSE_FILE) {
  701.          for (;;) {
  702.              exhdr = findrec();
  703.              userec(exhdr);
  704.              if (!exhdr->ext_hdr.xh_isextended) {
  705.                  break;
  706.              }
  707.          }
  708.      }
  709. }
  710.  
  711. /*
  712.  * Decode the mode string from a stat entry into a 9-char string and a null.
  713.  */
  714. void
  715. demode(mode, string)
  716.     register unsigned mode;
  717.     register char *string;
  718. {
  719.     register unsigned mask;
  720.     register char *rwx = "rwxrwxrwx";
  721.  
  722.     for (mask = 0400; mask != 0; mask >>= 1) {
  723.         if (mode & mask)
  724.             *string++ = *rwx++;
  725.         else {
  726.             *string++ = '-';
  727.             rwx++;
  728.         }
  729.     }
  730.  
  731.     if (mode & S_ISUID)
  732.         if (string[-7] == 'x')
  733.             string[-7] = 's';
  734.         else
  735.             string[-7] = 'S';
  736.     if (mode & S_ISGID)
  737.         if (string[-4] == 'x')
  738.             string[-4] = 's';
  739.         else
  740.             string[-4] = 'S';
  741.     if (mode & S_ISVTX)
  742.         if (string[-1] == 'x')
  743.             string[-1] = 't';
  744.         else
  745.             string[-1] = 'T';
  746.     *string = '\0';
  747. }
  748.  
  749.  
  750. /*
  751.  *----------------------------------------------------------------------
  752.  *
  753.  * get_names --
  754.  *
  755.  *    Get the file and link names from an archive header.  Assumes that 
  756.  *    the archive has just been advanced past the record for the given 
  757.  *    header.
  758.  *
  759.  * Results:
  760.  *    None.
  761.  *
  762.  * Side effects:
  763.  *    Sets current_filename and current_linkname.  Advances the archive 
  764.  *    past any extended header records.
  765.  *
  766.  *----------------------------------------------------------------------
  767.  */
  768.  
  769. void
  770. get_names(header)
  771.     union record *header;
  772. {
  773.     static char filename_buf[MAXPATHLEN];
  774.     static char linkname_buf[MAXPATHLEN];
  775.  
  776.     assert(header == head);
  777.     assert((head->header.isextended &
  778.         ~(XH_FILENAME|XH_LINKNAME|XH_SPARSE_FILE)) == 0);
  779.     if (head->header.isextended) {
  780.         assert(gnu_extended_header(head));
  781.     }
  782.  
  783.     /* 
  784.      * If there are no extended header records, just use what's in 
  785.      * the regular header.  Otherwise, pull the name out of the 
  786.      * extended header record(s).  Note that it is important that 
  787.      * get_short_name copy the string, rather than just save a pointer. 
  788.      * This is because the buffer package can relocate the header 
  789.      * record when get_long_name is called.
  790.      * 
  791.      * For each set of extended records, copy out the name into the 
  792.      * buffer.  Assume that the file name comes before the link 
  793.      * name (XXX).
  794.      */
  795.     
  796.     if (head->header.isextended & XH_FILENAME) {
  797.         get_long_name(XH_FILENAME, MAXPATHLEN, filename_buf);
  798.     } else {
  799.         get_short_name(NAMSIZ, head->header.name, MAXPATHLEN,
  800.                    filename_buf);
  801.     }
  802.     if (head->header.isextended & XH_LINKNAME) {
  803.         get_long_name(XH_LINKNAME, MAXPATHLEN, linkname_buf);
  804.     } else {
  805.         get_short_name(NAMSIZ, head->header.linkname, MAXPATHLEN,
  806.                    linkname_buf);
  807.     }
  808.  
  809.     if (current_filename) {
  810.         free(current_filename);
  811.     }
  812.     if (current_linkname) {
  813.         free(current_linkname);
  814.     }
  815.     current_filename = strdup(filename_buf);
  816.     current_linkname = strdup(linkname_buf);
  817.     return;
  818. }
  819.  
  820.  
  821. /*
  822.  *----------------------------------------------------------------------
  823.  *
  824.  * get_long_name --
  825.  *
  826.  *    Get one name from one or more extended header records.
  827.  *
  828.  * Results:
  829.  *    Fills in the given buffer.  
  830.  *
  831.  * Side effects:
  832.  *    Advances the archive.  Can cause a saved record from the archive to
  833.  *    get moved.
  834.  *
  835.  *----------------------------------------------------------------------
  836.  */
  837.  
  838. void
  839. get_long_name(type, buflen, buf)
  840.     int type;        /* expected name type */
  841.     int buflen;        /* number of characters in buf */
  842.     char *buf;        /* buffer to fill in */
  843. {
  844.     union record *exhdr;    /* ptr to extended header */
  845.  
  846.     buf[0] = '\0';
  847.  
  848.     for (;;) {
  849.         exhdr = findrec();
  850.         assert(exhdr->ext_hdr.xh_magic == XH_MAGIC);
  851.         assert(exhdr->ext_hdr.xh_type == type);
  852.         assert(strlen(buf) + strlen(exhdr->ext_hdr.xh_namebuf)
  853.                < buflen);
  854.         strcat(buf, exhdr->ext_hdr.xh_namebuf);
  855.         userec(exhdr);
  856.         if (!exhdr->ext_hdr.xh_isextended) {
  857.             break;
  858.         }
  859.     }
  860.  
  861.     assert(strlen(buf) < buflen);
  862. }
  863.  
  864.  
  865. /*
  866.  *----------------------------------------------------------------------
  867.  *
  868.  * get_short_name --
  869.  *
  870.  *    Get a "short" name from a header record.
  871.  *
  872.  * Results:
  873.  *    Fills in the "to" buffer with a null-terminated string.
  874.  *
  875.  * Side effects:
  876.  *    None.
  877.  *
  878.  *----------------------------------------------------------------------
  879.  */
  880.  
  881. void
  882. get_short_name(fromsize, frombuf, tosize, tobuf)
  883.     int fromsize;        /* number of bytes in input buffer */
  884.     char *frombuf;        /* buffer to copy from; maybe not 
  885.                  * null-terminated */
  886.     int tosize;        /* number of bytes in output buffer*/
  887.     char *tobuf;        /* buffer to copy to */
  888. {
  889.     assert(tosize >= fromsize + 1);
  890.  
  891.     strncpy(tobuf, frombuf, fromsize);
  892.     tobuf[fromsize] = '\0';
  893. }
  894. @
  895.  
  896.  
  897. 1.3
  898. log
  899. @Changes for long file names.  Don't abort after checksum error.
  900. (Mike checking in for Bob).
  901. @
  902. text
  903. @d31 1
  904. a54 3
  905. #ifdef ALLOW_LONG_NAMES
  906. extern char *get_long_name();
  907. #endif
  908. d64 3
  909. a79 2
  910.     char save_linkflag;
  911.     char *filename;
  912. d93 4
  913. a96 6
  914. #ifdef ALLOW_LONG_NAMES
  915.             filename = get_long_name(head, XH_FILENAME);
  916. #else
  917.             filename = head->header.name;
  918. #endif
  919.             if (   !name_match(filename)
  920. d98 7
  921. a104 11
  922.                 || (f_exclude && check_exclude(filename))) {
  923.  
  924.                 int isextended = 0;
  925.  
  926.                 /* Skip past it in the archive */
  927.                 if (head->header.isextended)
  928.                     isextended = 1;
  929.                 save_linkflag = head->header.linkflag;
  930.                 userec(head);
  931.                 if (isextended) {
  932.                     skip_extended_headers();
  933. d106 1
  934. a106 2
  935.                 /* Skip to the next header on the archive */
  936.                 if(save_linkflag != LF_DIR)
  937. d108 1
  938. d114 1
  939. a159 1
  940.     int    isextended = 0; /* Flag to remember if head is extended */
  941. a160 3
  942.     /* Save the record */
  943.     saverec(&head);
  944.  
  945. a174 1
  946.         userec(head);
  947. d176 1
  948. a176 5
  949. #ifdef ALLOW_LONG_NAMES            
  950.             save_name = get_long_name(head, XH_FILENAME);
  951. #else
  952.             save_name = head->header.name;
  953. #endif            
  954. d194 2
  955. a195 1
  956.                 msg_perror("only wrote %ld of %ld bytes to file %s",check, written,head->header.name);
  957. a201 1
  958.         saverec((union record **) 0);    /* Unsave it */
  959. a206 1
  960.     saverec((union record **) 0);    /* Unsave it */
  961. d208 1
  962. a208 23
  963.     if (head->header.isextended) 
  964.         isextended = 1;
  965.  
  966.     /* Skip past the header in the archive */
  967.     userec(head);
  968.  
  969.     /*
  970.      * If we needed to skip any extended headers, do so now, by
  971.      * reading extended headers and skipping past them in the 
  972.      * archive.
  973.      */
  974.     if (isextended) {
  975. /*        register union record *exhdr;
  976.  
  977.         for (;;) {
  978.             exhdr = findrec();
  979.  
  980.             if (!exhdr->ext_hdr.isextended) {
  981.                 userec(exhdr);
  982.                 break;
  983.             }
  984.             userec(exhdr);
  985.         }*/
  986. a209 1
  987.     }
  988. d212 1
  989. a212 5
  990. #ifdef ALLOW_LONG_NAMES            
  991.         save_name = get_long_name(head, XH_FILENAME);
  992. #else
  993.         save_name = head->header.name;
  994. #endif            
  995. d214 1
  996. d414 1
  997. a414 3
  998. #ifdef ALLOW_LONG_NAMES
  999.         char *n = get_long_name(head, XH_FILENAME);        
  1000.         name=quote_copy_string(n);
  1001. d416 1
  1002. a416 6
  1003.             name = n;
  1004. #else        
  1005.         name=quote_copy_string(head->header.name);
  1006.         if(name==0)
  1007.             name=head->header.name;
  1008. #endif        
  1009. d418 1
  1010. a418 1
  1011.         if(name!=head->header.name)
  1012. d437 2
  1013. a438 8
  1014. #ifdef ALLOW_LONG_NAMES
  1015.         if ('/' ==
  1016.           head->header.name[strlen(get_long_name(head,XH_FILENAME))-1])
  1017.             modes[0] = 'd';
  1018. #else            
  1019.         if ('/' == head->header.name[strlen(head->header.name)-1])
  1020.             modes[0] = 'd';
  1021. #endif        
  1022. d493 1
  1023. a493 6
  1024. #ifdef ALLOW_LONG_NAMES        
  1025.         name = quote_copy_string(get_long_name(head, XH_FILENAME));
  1026.         if(!name)
  1027.             name=get_long_name(head, XH_FILENAME);
  1028. #else        
  1029.         name = quote_copy_string(head->header.name);
  1030. d495 2
  1031. a496 3
  1032.             name=head->header.name;
  1033. #endif        
  1034.         fprintf(msg_file, "%s %s/%s %*s%s %s %s %.*s",
  1035. a503 1
  1036.             sizeof(head->header.name),
  1037. d506 1
  1038. a506 1
  1039.         if(name!=head->header.name)
  1040. d513 1
  1041. a513 6
  1042. #ifdef ALLOW_LONG_NAMES        
  1043.             name = quote_copy_string(get_long_name(head, XH_LINKNAME));
  1044.             if(!name)
  1045.             name=get_long_name(head, XH_LINKNAME);
  1046. #else        
  1047.             name=quote_copy_string(head->header.linkname);
  1048. d515 1
  1049. a515 2
  1050.                 name=head->header.linkname;
  1051. #endif            
  1052. d517 1
  1053. a517 1
  1054.             if(name!=head->header.linkname)
  1055. d522 1
  1056. a522 6
  1057. #ifdef ALLOW_LONG_NAMES        
  1058.             name = quote_copy_string(get_long_name(head, XH_LINKNAME));
  1059.             if(!name)
  1060.             name=get_long_name(head, XH_LINKNAME);
  1061. #else        
  1062.             name=quote_copy_string(head->header.linkname);
  1063. d524 3
  1064. a526 4
  1065.                 name=head->header.linkname;
  1066. #endif            
  1067.             fprintf(msg_file, " link to %s\n", head->header.linkname);
  1068.             if(name!=head->header.linkname)
  1069. d627 17
  1070. d649 1
  1071. d655 14
  1072. a668 8
  1073.     for (;;) {
  1074.         exhdr = findrec();
  1075.         userec(exhdr);
  1076.         if (!exhdr->ext_hdr.xh_isextended) {
  1077.         break;
  1078.         }
  1079.     }
  1080.     userec(exhdr);
  1081. d709 59
  1082. a767 3
  1083. #ifdef ALLOW_LONG_NAMES
  1084. void
  1085. foobar() { return; }
  1086. d769 36
  1087. a804 12
  1088. char *
  1089. get_long_name(header, type)
  1090.     union record *header;
  1091.     int type;
  1092. {
  1093.     static char buf[MAXPATHLEN];
  1094.     union record *exhdr;
  1095.  
  1096.     assert(header == head);
  1097.     assert(type == XH_FILENAME || type == XH_LINKNAME);
  1098.     assert((head->header.isextended &
  1099.         ~(XH_FILENAME|XH_LINKNAME|XH_SPARSE_FILE)) == 0);
  1100. a805 1
  1101.     if (head->header.isextended & type) {
  1102. d807 12
  1103. a818 3
  1104.     if (ar_record == head) {
  1105.         saverec(&head);
  1106.         userec(head);
  1107. d820 33
  1108. a852 19
  1109.     do {
  1110.         exhdr = findrec();
  1111.         if (exhdr->ext_hdr.xh_magic != XH_MAGIC) {
  1112.         foobar();
  1113.         }
  1114.         assert(exhdr->ext_hdr.xh_magic == XH_MAGIC);
  1115.         if (exhdr->ext_hdr.xh_type == type) {
  1116.         strcat(buf, exhdr->ext_hdr.xh_namebuf);
  1117.         assert(strlen(buf) < sizeof(buf));
  1118.         }
  1119.     } while (exhdr->ext_hdr.xh_isextended);
  1120.     assert(strlen(buf) < MAXPATHLEN);
  1121.     return buf;
  1122.     }
  1123.     switch (type) {
  1124.     case XH_FILENAME:   return head->header.name;
  1125.     case XH_LINKNAME:   return head->header.linkname;
  1126.     default:            abort();
  1127.     }
  1128. a853 1
  1129. #endif
  1130. @
  1131.  
  1132.  
  1133. 1.2
  1134. log
  1135. @Added support for long filenames and long symbolic linkes.
  1136. @
  1137. text
  1138. @d137 1
  1139. a137 1
  1140.                     abort();
  1141. a288 1
  1142.  
  1143. a289 1
  1144.  
  1145. d706 5
  1146. d713 1
  1147. a714 1
  1148.         userec(exhdr);
  1149. d760 3
  1150. d764 2
  1151. a765 2
  1152. get_long_name(head, type)
  1153.     union record *head;
  1154. d771 1
  1155. d773 3
  1156. d778 4
  1157. a781 1
  1158.     userec(head);
  1159. d784 3
  1160. @
  1161.  
  1162.  
  1163. 1.1
  1164. log
  1165. @Initial revision
  1166. @
  1167. text
  1168. @d36 2
  1169. d54 3
  1170. d80 2
  1171. a81 1
  1172.     
  1173. d94 8
  1174. a101 4
  1175.             
  1176.             if (   !name_match(head->header.name)
  1177.                  || (f_new_files && hstat.st_mtime<new_time)
  1178.                  || (f_exclude && check_exclude(head->header.name))) {
  1179. d104 1
  1180. a104 1
  1181.                 
  1182. d111 1
  1183. a111 11
  1184. /*                    register union record *exhdr;
  1185.  
  1186.                     for (;;) {
  1187.                         exhdr = findrec();
  1188.                         if (!exhdr->ext_hdr.isextended) {
  1189.                             userec(exhdr);
  1190.                             break;
  1191.                         }
  1192.                     }
  1193.                     userec(exhdr);*/
  1194.                     skip_extended_headers();
  1195. d137 1
  1196. d167 1
  1197. a167 1
  1198.     
  1199. d187 3
  1200. d191 1
  1201. d226 1
  1202. a226 1
  1203.         
  1204. d231 2
  1205. a232 2
  1206.       * If we needed to skip any extended headers, do so now, by
  1207.       * reading extended headers and skipping past them in the 
  1208. d249 8
  1209. a256 3
  1210.             
  1211.     if(f_multivol)
  1212.         save_name=head->header.name;
  1213. d258 1
  1214. a258 1
  1215.         
  1216. d260 1
  1217. a260 1
  1218.         
  1219. d458 6
  1220. a463 1
  1221.  
  1222. d467 1
  1223. d487 12
  1224. a498 4
  1225.                 modes[0] = '-'; 
  1226.                 if ('/' == head->header.name[strlen(head->header.name)-1])
  1227.                     modes[0] = 'd';
  1228.                 break;
  1229. d550 5
  1230. d558 1
  1231. d573 3
  1232. d577 5
  1233. d585 1
  1234. d592 5
  1235. d600 1
  1236. d619 4
  1237. a622 1
  1238.         case LF_DUMPDIR:
  1239. d710 3
  1240. a712 3
  1241.         if (!exhdr->ext_hdr.isextended) {
  1242.             userec(exhdr);
  1243.             break;
  1244. d755 32
  1245. @
  1246.